#include <iostream>
#include <thread>
#include <unistd.h>
#include <mutex>
using namespace std;
constexpr int MAX_NUM = 10000;
int number;
mutex mtx;
void fun(string name)
{
  for(int i=0; i<MAX_NUM; ++i)
  {
    mtx.lock();
    ++number;
    cout << "Thread "+name+" id = " << this_thread::get_id() << " number = " << number << endl;
    mtx.unlock();
    usleep(10);
  }
}
void funA()
{
  for(int i=0; i<MAX_NUM; ++i)
  {
    mtx.lock();
    ++number;
    cout << "Thread A id = " << this_thread::get_id() << " number = " << number << endl;
    mtx.unlock();
    usleep(10);
  }
}
void funB()
{
  for(int i=0; i<MAX_NUM; ++i)
  {
    mtx.lock();
    ++number;
    cout << "Thread B id = " << this_thread::get_id() << " number = " << number << endl;
    mtx.unlock();
    usleep(10);
  }
}
void funC()
{
  for(int i=0; i<MAX_NUM; ++i)
  {
    mtx.lock();
    ++number;
    cout << "Thread C id = " << this_thread::get_id() << " number = " << number << endl;
    mtx.unlock();
    usleep(10);
  }
}

int main()
{
  thread thr1(fun, "A");
  thread thr2(fun, "B");
  thread thr3(fun, "C");
  thr1.join();
  thr2.join();
  thr3.join();

  cout << "thread1 and thread2 and thread3 completed." << endl;
  return 0;

}
